package com.zztl.infiniteBand.ui.view

import android.content.Context
import android.graphics.Canvas

import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.Interpolator
import android.view.animation.LinearInterpolator


/**
 *  @desc
 *  @auth ${user}
 *  @time 2018/10/24  17:45
 */
/**
 * 水波纹特效
 * Created by hackware on 2016/6/17.
 */
class WaveView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    var mInitialRadius: Float = 0.toFloat()   // 初始波纹半径
    var mMaxRadiusRate = 0.85f   // 如果没有设置mMaxRadius，可mMaxRadius = 最小长度 * mMaxRadiusRate;
    var mMaxRadius: Float = 0.toFloat()   // 最大波纹半径
    var mDuration: Long = 2000 // 一个波纹从创建到消失的持续时间
    var mSpeed = 1000   // 波纹的创建速度，每500ms创建一个
    var interpolator: Interpolator = LinearInterpolator()

    var isFirstDraw = true


    private val mCircleList = ArrayList<Circle>()
    private var mIsRunning: Boolean = false

    private var mMaxRadiusSet: Boolean = false

    private val mPaint: Paint
    private var mLastCreateTime: Long = 0

    private val mCreateCircle = object : Runnable {
        override fun run() {
            if (mIsRunning) {
                newCircle()
                postDelayed(this, mSpeed.toLong())
            }
        }
    }

    init {
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        setStyle(Paint.Style.FILL)
//        val circle = Circle()
    }

    fun setStyle(style: Paint.Style) {
        mPaint.style = style
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        if (!mMaxRadiusSet) {
            mMaxRadius = Math.min(w, h) * mMaxRadiusRate / 2.0f
        }
    }


    fun setColor(color: Int) {
        mPaint.color = color
    }

    /**
     * 开始
     */
    fun start() {
        if (!mIsRunning) {
            mIsRunning = true
            isFirstDraw = false
            mCreateCircle.run()
        }
    }

    /**
     * 停止
     */
    fun stop() {
        mIsRunning = false
        mCircleList.clear()
        mPaint.alpha=255
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        if (mCircleList.size == 0) {
            canvas.drawCircle(width / 2.toFloat(), height / 2.toFloat(), mMaxRadius / 3 * 2, mPaint)
            return
        }
        val iterator = mCircleList.iterator()
        while (iterator.hasNext()) {
            val circle = iterator.next()
            if (System.currentTimeMillis() - circle.mCreateTime < mDuration) {
                mPaint.alpha = circle.alpha
                canvas.drawCircle(width / 2.toFloat(), height / 2.toFloat(), circle.currentRadius, mPaint)
            } else {
                iterator.remove()
            }
        }
        if (mCircleList.size > 0) {
            postInvalidateDelayed(30)
        }
    }


    private fun newCircle() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - mLastCreateTime < mSpeed) {
            return
        }
        val circle = Circle()
        mCircleList.add(circle)
        invalidate()
        mLastCreateTime = currentTime
    }

    private inner class Circle {
        val mCreateTime = System.currentTimeMillis()
        val alpha: Int
            get() {
                val percent = (System.currentTimeMillis() - mCreateTime) * 1.0f / mDuration
//               return (1.0f- (interpolator.javaClass.getMethod("getInterpolation",Float::class.java)?.invoke(interpolator,percent)).toString().toFloat()*255).toInt()
                return ((1.0f - interpolator.getInterpolation(percent)) * 255).toInt()
            }

        val currentRadius: Float
            get() {
                val percent = (System.currentTimeMillis() - mCreateTime) * 1.0f / mDuration
                return mInitialRadius + interpolator.getInterpolation(percent) * (mMaxRadius - mInitialRadius)
//                return mInitialRadius + interpolator.getInterpolation(percent) * mMaxRadius
//                return mInitialRadius + (interpolator.javaClass.getMethod("getInterpolation",Float::class.java)?.invoke(interpolator,percent)).toString().toFloat()* (mMaxRadius - mInitialRadius)
            }

    }

    override fun onDetachedFromWindow() {
        mIsRunning=false
        super.onDetachedFromWindow()
    }

}

